package com.cysion.study.ui.personal

import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.study.R
import com.cysion.study.adapter.ClasssAdapter
import com.cysion.study.entity.AllResearchStuEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box.context
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_pending_history.*
import kotlinx.android.synthetic.main.activity_pending_history.rv_list
import kotlinx.android.synthetic.main.activity_pending_history.smartLayout
import kotlinx.android.synthetic.main.activity_pending_history.topbar

//课程列表的课时列表
class ClassManageActivity : BaseActivity() {
    private var mdatalist: MutableList<String> = mutableListOf()
    private lateinit var pendingAdapter: ClasssAdapter
    private var curPage = 1
    private var category = 0//0.待我审核 1.抄送我的
    override fun getLayoutId(): Int = R.layout.activity_pending_history

    private var cid=""
    private var researchId=""
    private var className=""
    override fun initView() {
        cid=intent.extras.getString("cid")
        researchId=intent.extras.getString("researchId")
        className=intent.extras.getString("className")
        darkTextTheme(color(R.color.white))
        topbar.apply {
            setTitleBackColor(color(R.color.white))
            setTitle(className)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()
    }



    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
//        smartLayout.finishRefresh()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
//        smartLayout.setEnableLoadMore(false)
        smartLayout.setOnRefreshListener {
            getDataList(curPage)
            smartLayout.setEnableLoadMore(true)
        }
        smartLayout.setOnLoadMoreListener {

            getDataList(curPage)
        }

        //模拟数据
        /* if (classAdapter.itemCount == 0) {
             var lists: MutableList<String> = mutableListOf("","")
             mSourceList.addAll(lists)
             classAdapter.notifyDataSetChanged()
         }*/
    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        pendingAdapter = ClasssAdapter(dataList, context)
        rv_list.adapter = pendingAdapter
        rv_list.layoutManager = LinearLayoutManager(context)
        pendingAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {//研学内容列表
                var bundle = Bundle()
                bundle.putString("courseId",obj.id)//课时id
                UIHelper.jump(this,ClassDetailActivity::class.java,bundle)//课程详情
            }
        }
        //模拟数据
        /*if (pendingAdapter.itemCount == 0) {
            var lists: MutableList<String> = mutableListOf("","")
            mdatalist.addAll(lists)
            pendingAdapter.notifyDataSetChanged()
        }*/
    }

    private val dataList: MutableList<AllResearchStuEntity> = mutableListOf()

    private fun getDataList(page: Int) {
        Alert.loading(this)
        ApiClient.api.getAllCourseList(cid.toInt(),researchId.toInt(),curPage,UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    CLog.e("@@@=" + pendingAdapter.itemCount)

                    /* if (classAdapter.itemCount == 0) {
                         var lists: MutableList<String> = mutableListOf("","")
                         mSourceList.addAll(lists)
                         classAdapter.notifyDataSetChanged()
                     }*/
                    if (curPage == 1) {
                        dataList.clear()
                        if (it.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        pendingAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }



                }
                _onError {
                    Alert.close()
                    stopRefresh()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }


    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }
    private fun noticeRead(tid: String, remark: String, flag:Int) {
        // flag1.同意 2.拒绝
        ApiClient.api.approvalDeal(
            UserCache.token, tid, remark,flag.toString()
        ).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    ShowToast("操作成功")
//                    presenter.getList(curPage, 0)
                    Alert.close()
                }
                _onError {
                    //                        Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }



    override fun closeMvp() {
    }

}
